package com.code.petapp.activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SearchView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.code.petapp.R;
import com.code.petapp.adapter.OrderImgAdapter;
import com.code.petapp.bean.AdoptBean;
import com.code.petapp.bean.OrderBean;
import com.code.petapp.databinding.ActivityOrderBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.AdoptUtils;
import com.code.petapp.util.OrderUtils;
import com.code.petapp.util.SPCommonUtils;

import java.util.ArrayList;
import java.util.List;

public class OrderActivity extends BaseActivity implements OrderImgAdapter.OnItemClickListener {
    ActivityOrderBinding binding;
    private RecyclerView recyclerView;//声明RecyclerView
    private OrderImgAdapter adapter1;
    private Context context;
    private List<OrderBean> list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityOrderBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        context = this;
        recyclerView = binding.recView;
        list = new ArrayList<>();
        load();
        search();
        adapter1=new OrderImgAdapter(context,list);
        adapter1.setOnItemClickListener(this);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter1);
    }
    private void load(){
        list.clear();
        OrderUtils.selectAll(SPCommonUtils.getInstance().getToken() ,new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(ArrayList<OrderBean> beanList) {
                list.addAll(beanList);
                adapter1.notifyDataSetChanged();
            }

            @Override
            public void error(int code, String error) {
                toastShortShow(error);
            }
        });
    }
    private void search(){
        binding.searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                searchData(s);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                searchData(s);
                return false;
            }
        });
    }
    private void searchData(String s){
        load(s);
    }
    private void load(String orderId) {
        list.clear();
        OrderUtils.selectOrder(SPCommonUtils.getInstance().getToken(), orderId, new HttpListener<>() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void success(ArrayList<OrderBean> beanList) {
                list.addAll(beanList);
                adapter1.notifyDataSetChanged();
            }

            @Override
            public void error(int code, String error) {
                toastShortShow(error);
            }
        });
    }

    @Override
    public void onClick(View parent, int position) {
        if(list.get(position).getStatus().equals("待发货")){
            toastShortShow("该订单未发货");
        }else{
            AlertDialog.Builder builder = new AlertDialog.Builder(OrderActivity.this);
            builder.setIcon(R.drawable.icon);
            builder.setTitle("确认收货？");
            builder.setMessage("您确定已经收到商品了吗");
            builder.setPositiveButton("确定",
                            (DialogInterface dialog, int arg1) -> {
                                dialog.dismiss();
                                OrderBean orderBean=new OrderBean();
                                orderBean.setId(list.get(position).getId());
                                orderBean.setStatus("已完成");
                                OrderUtils.update(SPCommonUtils.getInstance().getToken(), orderBean, new HttpListener<>() {
                                    @Override
                                    public void success(String data) {
                                        load();
                                        toastShortShow("收货成功");
                                    }

                                    @Override
                                    public void error(int code, String error) {
                                        toastShortShow(error);
                                    }});
                            })
                    .setNegativeButton("取消",
                            (DialogInterface dialog, int arg1) -> {
                                dialog.dismiss();
                            });
            builder.create().show();
        }
    }

    @Override
    public void onClickMore(View parent, int position) {
        Intent intent=new Intent(OrderActivity.this,OrderDetailActivity.class);
        int num=list.get(position).getNum();
        String time=list.get(position).getTime();
        String address=list.get(position).getAddress();
        String phone=list.get(position).getPhone();
        String status=list.get(position).getStatus();
        String goodsName=list.get(position).getGoodsName();
        String orderId=list.get(position).getOrderId();
        String orderName=SPCommonUtils.getInstance().getName();
        String imgUrl=list.get(position).getGoodsImg();
        String userName=list.get(position).getUsername();
        Double price=list.get(position).getPrice();
        intent.putExtra("num",num);
        intent.putExtra("time",time);
        intent.putExtra("address",address);
        intent.putExtra("phone",phone);
        intent.putExtra("status",status);
        intent.putExtra("orderId",orderId);
        intent.putExtra("goodsName",goodsName);
        intent.putExtra("orderName",orderName);
        intent.putExtra("price",price);
        intent.putExtra("imgUrl",imgUrl);
        intent.putExtra("userName",userName);
        startActivity(intent);

    }
}